#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<iostream>
#include<unordered_map>
#include<unordered_set>
using namespace std;

/*
    2022 4-17 网易雷火面试题
    
*/

class logicClause{
public:
    logicClause(string &logit){
        name = logit;
        op = true;
        isLeaf = true;
        left = right = nullptr;
    }

    bool search_match(unordered_set<string> &words){
        if(isLeaf)return name==""||words.find(name) != words;
        if(op){
            bool res=false;
            if(left)res = left->search_match(words);
            // else res = right->search_match(words);
            if(!res)return false;
            // if(!left)return res;
            if(!right)return res;
            return right->search_match(words);
        }
        if(right)return left->search_match(words)||right->search_match(words);
        return left->search_match(words);
    }
    bool op;// true为and, false为or操作
    bool isLeaf;
    string name;
    logicClause* left;
    logicClause* right;
};

namespace netease{
namespace April_17_2022{
    class solve_net_ease4_zkm{
    public:
        solve_net_ease4_zkm(){

        }

        void get_data(vector<string> &question, vector<string> &match){
            question = {"zhuangbei xilian citiao","wanjia shangdian ruhe kaidian","zhuangbei dazao","lingshou tuijian","yishi he shangdian de qubie","shangdian pochan"};
            match = {"zhuangbei","zhuangbei and citiao","yishi or shangdian","daoke and lingshou and tuijian","yishi and kaidian or shangdian and kaidian","( xishi or shangdian ) and ( daobi or pochan )"};
        }

        unordered_set<string> partionSentence(string sentence){
            unordered_set<string> res;
            string cur;
            for(char c:sentence){
                if(c == ' '){
                    res.insert(cur);
                    cur = "";
                }
                cur += c;
            }
            res.insert(cur);
            return res;
        }



        vector<string> maxMatch(vector<string> &question, vector<string> &match){
            int qsz = question.size();
            vector<unordered_set<string>> qs(qsz);
            for(int i = 0; i < qsz; ++i){
                qs[i] = partionSentence(question[i]);
            }
            vector<logicClause> match_logics;
            for(string &m:match){
                match_logics.push_back(logicClause(m));
            }
            vector<string> ans;
            for(int i=0;i<qsz;++i){
                bool find = false;
                string res;
                for(int j =0; j<match_logics.size();++j){
                    if(match_logics[j].search_match(qs[i])&&match[j].length()>res.length()){
                        find = true;
                        res = match[j];
                    }
                }
                ans.push_back(res);
            }
            return ans;
        }

    };
}
}